(function(){
var _location = location,
    hash = _location.hash,
    $ = jQuery,
    cfg = _pp_parking,
    debug = (cfg.debug || false),
    current_url = _location.href,
    _http = 'http://',
    _blog_yam_com = 'blog.yam.com',
    channel_url_map = [
        [_http + 'album.' + _blog_yam_com + '/{blog_id}', 'album'],
        [_http + 'mymedia.yam.com/{blog_id}', 'mymedia'],
        [_http + 'diary.' + _blog_yam_com + '/{blog_id}', 'diary'],
        [_http + _blog_yam_com + '/msg/{blog_id}', 'msg'],
        [_http + _blog_yam_com + '/user/{blog_id}.html', 'user'],
        [_http + _blog_yam_com + '/{blog_id}', 'blog']
    ];

var fix_url = function(url)
{
        // http://blog.yam.com/BlogIndex.php?BLOG_ID=girvan&CATEGORY_ID=1472132
        return url.replace(/user_data\.php\?BLOG_ID=([\w\-]+)/, 'user/$1.html')
                .replace(/BlogIndex\.php\?BLOG_ID=([\w\-]+)&CATEGORY_ID=(\d+)/, '$1/category/$2');
};

if(current_url.indexOf('domain=')===-1)
{
    for(var i=0,n=channel_url_map.length; i < n; i++)
    {
        var map_url = channel_url_map[i][0].replace('{blog_id}', cfg.blog_id),
            map_channel = channel_url_map[i][1];

        current_url = fix_url(current_url);

        if(current_url.indexOf(map_url)===0)
        {
            var target_url = current_url.replace(map_url, 'http://' + cfg.domain + '/' + map_channel);

            if(debug)
                alert(target_url);
            else
                top.location.href = target_url;
            return;
        }
    }
}
else
{

    // setup title
    $('body').prepend('<iframe width=1 height=1 style="display:none;" src="http://' + cfg.domain + "/_static/set_meta.html#title=" + encodeURIComponent(document.title) + '"></iframe>');

    $("body").delegate('a', 'click', function(e){
    
        var domain = hash.match(/domain=([^&=]+)/)[1],
            blog_id = hash.match(/blog_id=([^&=]+)/)[1],
            _this = $(this),
            link = _this.attr('href'),
            sub_folder = "";

        // ##### fix format
        link = fix_url(link);
        // convert relative link to absolute link
        if(link.match(/^\/[^\/]/))
            link = 'http://' + _location.host + link;

        if(debug)
             e.preventDefault();

        // this is not a link
        if(!link || link === '#' || link.indexOf('javascript:')!=-1)
            return true;

        // default _top
        _this.attr('target', '_top');
        
        for(var i=0,n=channel_url_map.length; i < n; i++)
        {
            var pattern = channel_url_map[i][0], channel = channel_url_map[i][1];
            pattern = pattern.replace('{blog_id}', blog_id);
            // console.log(link + "\t\t\t" + pattern);
            if(link.indexOf(pattern)!=-1)
            {
                // success
                link = link.replace(pattern, 'http://' + domain + '/' + channel);
                _this.attr('href', link);

                if(debug)
                    console.log("goto:\t" + link);
                return;
            }
        }

        if(debug)
            console.log('original:' + link);
        _this.attr('target', '_blank');
        
    });
 
    // focus current window or keyboard will be useless
    $(window).focus();
}

})();
